<?php

namespace App\Listeners;

use App\Events\OrderPaidEvent;
use App\Services\CountProductSales\CountProductSales;
use App\Supports\Product\ProductReturnBalance;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use App\Models\UserDivinationRecord;
use App\Models\PayConfig;
use App\Models\User;
use Carbon\Carbon;
use App\Services\FinishGroup\FinishGroup;

class OrderPaidListener
{
    public function handle(OrderPaidEvent $event)
    {
        $order = $event->order;
        $user = $order->user;

	    app(CountProductSales::class)->count($order);


       /* if($user->isNotValidVip()){

            return false;
        }*/
        if($order->record_id){
            $record =  UserDivinationRecord::find($order->record_id);
            $record->update(['is_show'=>true]);
        }

        if($order->pay_config_id){
            $config =  PayConfig::find($order->pay_config_id);
            if($config->status == "times"){

                if($config->type ==1){
                    $picTimes = $user->pic_times + $config->pic_num;
                    $picMergeTimes = $user->pic_merge_times + $config->pic_merge_num;

                    $user->update(['pic_times'=>$picTimes,'pic_merge_times'=>$picMergeTimes]);
                }else{

                    $allTimes = $user->times + $config->num - 1;
                    $user->update(['times'=>$allTimes]);
                }
               
            }else{

                $vipExpiredAt = $user->vip_expired_at ? $user->vip_expired_at->addDays($config->num) : Carbon::now()->addDays($config->num);
                $user->update(['grade'=>"vip","vip_expired_at"=>$vipExpiredAt]);

            }
        }


        $user->update(['order_num'=>$user->order_num + 1]);
       

    }
}
